﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace JoyeriaProject
{
    public partial class frmCustomer : Form
    {
        ModeloDatos MD = new ModeloDatos();

        public frmCustomer()
        {
            InitializeComponent();
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            if (txtNombre.Text != string.Empty && txtApellidos.Text != string.Empty && txtMonto.Text != string.Empty && combCiclo.SelectedItem != null && comboAgente.SelectedItem != null)
            {

                MD.Client = new Clientes
                {
                    CustomerID = (MD.GetClients().Max(n => n.CustomerID) + 1),
                    Fecha = DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/" + DateTime.Now.Year.ToString(),
                    Nombre = txtNombre.Text,
                    Apellidos = txtApellidos.Text,
                    TipoCasa = (radiobtnCasa.Checked ? "Propia" : "Rentada"),
                    Telefono = txtTel.Text,
                    Celular = txtCel.Text,
                    Direccion = txtDireccion.Text,
                    DetalleDireccion = txtDetallesDireccion.Text,
                    Colonia = txtColonia.Text,
                    Articulos = "",
                    DiaCobro = (comboDiaCobro.SelectedItem == "DOMINGO" ? 0 : comboDiaCobro.SelectedItem == "LUNES" ? 1 : comboDiaCobro.SelectedItem == "MARTES" ? 2 : comboDiaCobro.SelectedItem == "MIERCOLES" ? 3 : comboDiaCobro.SelectedItem == "JUEVES" ? 4 : comboDiaCobro.SelectedItem == "VIERNES" ? 5 : 6),
                    TipoAbono = (combCiclo.SelectedItem == "SEMANAL" ? 7 : combCiclo.SelectedItem == "CATORCENA" ? 14 : combCiclo.SelectedItem == "QUINCENAL" ? 15 : combCiclo.SelectedItem == "MENSUAL" ? 30 : combCiclo.SelectedItem == "BIMESTRAL" ? 60 : combCiclo.SelectedItem == "TRIMESTRAL" ? 90 : combCiclo.SelectedItem == "SEMESTRAL" ? 180 : combCiclo.SelectedItem == "ANUAL" ? 365 : 1),
                    MontoAbono = Convert.ToDouble(txtMonto.Text),
                    Referencia = txtReferencia.Text,
                    Compania = txtCompania.Text,
                    Puesto = txtPuesto.Text,
                    DirCompania = txtDirEmpleo.Text,
                    DirDetalleCompania = txtDetallesDirEmpleo.Text,
                    DirColoniaCompania = txtColoniaEmpleo.Text,
                    TelCompania = txtTelEmpleo.Text,
                    Agente = comboAgente.Text,
                    Ruta = txtRuta.Text
                };

                if (MD.Add<Clientes>(ref MD.Client))
                {
                    MessageBox.Show("El cliente se Registro Satisfactoriamente", "LISTO!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Dispose();
                }
                else
                {
                    MessageBox.Show("Hubo un problema al grabar el registro del cliente", ":(  HUBO UN PROBLEMA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else 
            {
                MessageBox.Show("Falta informacion importante por rellenar", "Falta Informacion", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            
            }
         }

        private void frmCustomer_Load(object sender, EventArgs e)
        {
            lblFechaActual.Text = DateTime.Now.ToString("D", new CultureInfo("es-MX"));

            try
            {
                lblNumFolio.Text = (MD.GetClients().Max(n => n.CustomerID) + 1).ToString();
       
                foreach (Agentes agente in MD.GetAgents())
                    comboAgente.Items.Add(agente.Nombre + " " + agente.Apellidos);

            }
            catch (Exception ex)
            {
               
            }

        }

       
    }
}
